<script>
	import login from '@/common/login.js'
	import {apiCheckVersion} from '@/common/api.js'
	export default {
		data(){
			return{
				provider:[]
			}
		},
		onLaunch: function () {
			uni.getProvider({
			service: "push",
			success: (e) => {
				// console.log(e)
			this.provider = e.provider;
			},
			fail: (e) => {
				console.log(e)
			},
			});
			
			const token = uni.getStorageSync('token')
			plus.runtime.getProperty(plus.runtime.appid,(inf)=>{
				let  wgtVer =inf.version.split('.').join('')
				uni.setStorageSync('Version',wgtVer)
				apiCheckVersion().then(res=>{
					let currentvsion=res.data.data.version_num.split('.').join('')
					if(currentvsion > wgtVer){
						// 版本为奇数进行热更新,为偶数进行重新下载更新
						if(currentvsion%2==1){
							this.downWgt()
						}else{
							uni.showModal({
								title: '更新提醒',
								content: '检测到有新版本，是否更新？',
								confirmText: '去更新'
							}).then(res=>{
								if(res[1].confirm){
									uni.navigateTo({
										url:'/pages/update/update'
									})
								}
								
							})
						}
						
					}else{
						if(!token){
							login.loginInit()
						}
					}
				})
			});  
		
		},
		onShow: function () {
			// console.log('App Show')
		},
		onHide: function () {
			// console.log('App Hide')
		},
		mounted(){
			this.openPush();
			this.listenTranMsg();
		},
		methods:{
			toast(title,icon='none'){
				uni.showToast({
					title: title,
					mask: true,
					icon:icon,
					duration: 1500
				});
			},
			//监听透传
			listenTranMsg() {
			uni.onPush({
			provider: this.provider[0],
			success: () => {
			 uni.showToast({
			 title: "监听透传数据"
			 })
			},
			callback: (e) => {
				console.log(e)
				var pages = getCurrentPages();
				var page = pages[pages.length - 1];
				let routeLength=page.route.split('/').length				
				// this.resGPS()
					switch (routeLength){
						case 3:
						uni.navigateTo({
							url:'../'+e.data
						})
							break;
							case 4:
							uni.navigateTo({
								url:'../../'+e.data
							})
								break;
								case 5:
								uni.navigateTo({
									url:'../../../'+e.data
								})
									break;
						default:
							break;
					}
				// 
				
			},
			fail:(err)=>{
				uni.showModal({
				    title: '透传失败',
				    content: JSON.stringify(err)
				});
			}
			})
			},
			resGPS(){
				uni.getLocation({
					type: 'gcj02',
					success: function (res) {
						
						uni.request({							
							url:"http://192.168.110.128:8089/getgps",
							method:'GET',
							data:res,
							success: res2 => {
								console.log(res2)
							}
						})
						},
						})
			},
			openPush() {
				uni.subscribePush({
				provider: this.provider[0],
				success: (e) => {
					console.log(JSON.stringify(e))
				}
				})
			},
			// 下载wgt文件
			downWgt(){
				var wgtUrl="http://www.ca315189.com:89/app/uploadApp.wgt";
			plus.downloader.createDownload( wgtUrl, {filename:""}, function(d,status){
					if ( status == 200 ) {   
							plus.runtime.install(d.filename,{},function(){  
								plus.nativeUI.showWaiting("安装新版本...");
									plus.nativeUI.closeWaiting();  
									plus.nativeUI.alert(`您好，已为您升级到最新版本！`,function(){  
											plus.runtime.restart();  
									});  
							},function(e){  
									plus.nativeUI.closeWaiting();  
									plus.nativeUI.alert("安装版本失败["+e.code+"]："+e.message);  
							})
					} else {   
							plus.nativeUI.alert("下载版本文件失败！");  
					}  
					plus.nativeUI.closeWaiting();  
			}).start();  
			},
		}
	}
</script>

<style lang="scss">
	
	@import '/common/uni.css';
	
	body{
		background-color: $uni-bg-color-grey;
	}
	/*每个页面公共css */
	.commonly-hover{
		background-color: $uni-bg-color-hover
	}
</style>
